<template>
  <div>
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      style="text-align: left"
    >
      <el-form-item label="设备名称">
        <el-input
          v-model="formInline.equipmentName"
          placeholder="设备名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="设备位置">
        <el-select v-model="formInline.equipmentLocaId" placeholder="设备位置">
          <el-option label="请选择" value=""></el-option>
          <el-option label="1栋" value="1"></el-option>
          <el-option label="2栋" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          style="background-color: green; color: white"
          icon="el-icon-search"
          @click="list"
          >查询</el-button
        >
        <el-button style="color: gray" icon="el-icon-refresh">重置</el-button>
        <el-button style="color: green" icon="el-icon-plus" @click="add"
          >新增</el-button
        >
        <el-button
          style="color: orange"
          icon="el-icon-download"
          @click="exportToExcel"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <template>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :row-class-name="tableRowClassName"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="序号" align="center">
        </el-table-column>
        <el-table-column prop="equipmentCoding" label="设备编码" align="center">
        </el-table-column>
        <el-table-column prop="equipmentName" label="设备名称" align="center">
        </el-table-column>
        <el-table-column prop="deviceType" label="设备类型" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.deviceType == 1">视频监控系统</span>
            <span v-if="scope.row.deviceType == 2">消防系统</span>
          </template>
        </el-table-column>
        <el-table-column prop="equipmentModel" label="型号" align="center">
        </el-table-column>
        <el-table-column prop="equipmentBrand" label="品牌" align="center">
        </el-table-column>
        <el-table-column prop="equipmentLocaId" label="设备位置" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.equipmentLocaId == 1">1号电梯</span>
            <span v-if="scope.row.equipmentLocaId == 2">2号电梯</span>
          </template>
        </el-table-column>
        <el-table-column prop="mainteCycle" label="维护周期" align="center">
        </el-table-column>
        <el-table-column prop="mainteDepartId" label="维护部门" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.mainteDepartId == 1">广州公司01</span>
            <span v-if="scope.row.mainteDepartId == 2">北京公司02</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" label="设备状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.state == true">正常</span>
            <span v-if="scope.row.state == false" style="color: red"
              >已报废</span
            >
          </template>
        </el-table-column>
        <el-table-column label="二维码" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="creatQrCode(scope.row.equipmentName)"
              >点击</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template>
            <el-button type="text" style="color: green" @click="examine"
              >查看</el-button
            >
            <el-button type="text" style="color: orange" @click="upd"
              >编辑</el-button
            >
            <el-button type="text" style="color: red" @click="del"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formInline.pageIndex"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="formInline.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="countnum"
    >
    </el-pagination>

    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        style="width: 500px; margin: 2px auto"
      >
        <el-form-item label="设备编码" prop="equipmentCoding">
          <el-input v-model="form.equipmentCoding"></el-input>
        </el-form-item>
        <el-form-item label="原厂编号" prop="originalNum">
          <el-input v-model="form.originalNum"></el-input>
        </el-form-item>
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input v-model="form.equipmentName"></el-input>
        </el-form-item>
        <el-form-item label="设备位置" prop="equipmentLocaId">
          <el-select
            style="width: 419px"
            v-model="form.equipmentLocaId"
            placeholder="请选择设备位置"
          >
            <el-option label="1号电梯" value="1"></el-option>
            <el-option label="2号电梯" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <el-select
            v-model="form.deviceType"
            placeholder="请选择设备类型"
            style="width: 419px"
          >
            <el-option label="视频监控系统" value="1"></el-option>
            <el-option label="消防系统" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细位置" prop="detailedLocation">
          <el-input v-model="form.detailedLocation"></el-input>
        </el-form-item>
        <el-form-item label="设备型号" prop="equipmentModel">
          <el-input v-model="form.equipmentModel"></el-input>
        </el-form-item>
        <el-form-item label="设备品牌" prop="equipmentBrand">
          <el-input v-model="form.equipmentBrand"></el-input>
        </el-form-item>
        <el-form-item label="购买日期" prop="purchaseDate">
          <el-col :span="11">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.purchaseDate"
              style="width: 419px"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="使用日期" prop="useDate">
          <el-col :span="11">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.useDate"
              style="width: 419px"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="维护时间" prop="startTime">
          <el-col :span="11">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.startTime"
              style="width: 419px"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="维护周期" prop="mainteCycle">
          <el-input v-model="form.mainteCycle"></el-input>
        </el-form-item>
        <el-form-item label="维护部门" prop="mainteDepartId">
          <el-select
            v-model="form.mainteDepartId"
            placeholder="请选择维护部门"
            style="width: 419px"
          >
            <el-option label="视频监控系统" value="1"></el-option>
            <el-option label="消防系统" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维护人员" prop="personnelId">
          <el-select
            v-model="form.personnelId"
            placeholder="请选择维护人员"
            style="width: 419px"
          >
            <el-option label="张三" value="1"></el-option>
            <el-option label="李四" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" style="color: green"
          >取 消</el-button
        >
        <el-button @click="baocun" style="color: green">保 存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="二维码" :visible.sync="dialogTableVisible" width="300px">
      <div class="qrcode divqr" ref="qrCodeUrl"></div>
    </el-dialog>

    <el-dialog
      width="600px"
      title="详情信息"
      :visible.sync="dialogFormVisible1"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="180px"
        style="width: 600px; margin: 2px auto"
      >
        <el-form-item label="设备编码" prop="equipmentCoding">
          {{ form.equipmentCoding }}
        </el-form-item>
        <el-form-item label="原厂编号" prop="originalNum">
          {{ form.originalNum }}
        </el-form-item>
        <el-form-item label="设备名称" prop="equipmentName">
          {{ form.equipmentName }}
        </el-form-item>
        <el-form-item label="设备位置" prop="equipmentLocaId">
          {{ form.equipmentLocaId }}
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          {{ form.deviceType }}
        </el-form-item>
        <el-form-item label="详细位置" prop="detailedLocation">
          {{ form.detailedLocation }}
        </el-form-item>
        <el-form-item label="设备型号" prop="equipmentModel">
          {{ form.equipmentModel }}
        </el-form-item>
        <el-form-item label="设备品牌" prop="equipmentBrand">
          {{ form.equipmentBrand }}
        </el-form-item>
        <el-form-item label="购买日期" prop="purchaseDate">
          {{ form.purchaseDate }}
        </el-form-item>
        <el-form-item label="使用日期" prop="useDate">
          {{ form.useDate }}
        </el-form-item>
        <el-form-item label="开始维护时间" prop="startTime">
          {{ form.startTime }}
        </el-form-item>
        <el-form-item label="维护周期" prop="mainteCycle">
          {{ form.mainteCycle }}
        </el-form-item>
        <el-form-item label="维护部门" prop="mainteDepartId">
          {{ form.mainteDepartId }}
        </el-form-item>
        <el-form-item label="维护人员" prop="personnelId">
          {{ form.personnelId }}
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import $ from "jquery";
import QRCode from "qrcodejs2";
import { GetShowEquipmentLedger } from "@/api/AssetManagecopy/EquipmentLedger";
import { deleteEquipmentLedger } from "@/api/AssetManagecopy/EquipmentLedger";
import { updEquipmentLedger } from "@/api/AssetManagecopy/EquipmentLedger";
import { GetTotaocount } from "@/api/AssetManagecopy/EquipmentLedger";
export default {
  data() {
    return {
      tableRowClassName({ row, rowIndex }) {
        if (rowIndex === 0) {
          return "warning-row";
        } else if (rowIndex === 2) {
          return "success-row";
        }
        return "";
      },
      tableData: [],
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogTableVisible: false,
      multipleSelection: [],
      ids: {},
      form: {
        id: "",
        equipmentCoding: "",
        originalNum: "",
        equipmentName: "",
        equipmentLocaId: "",
        deviceType: "",
        detailedLocation: "",
        equipmentModel: "",
        equipmentBrand: "",
        purchaseDate: "",
        useDate: "",
        startTime: "",
        mainteCycle: "",
        mainteDepartId: "",
        personnelId: "",
      },
      formInline: {
        pageIndex: 1,
        pageSize: 2,
        equipmentName: "",
        equipmentLocaId: "",
        equipmentName: "",
      },
      countnum: 0,
      count: 0,
    };
  },
  created() {
    this.list();
    this.totalcount();
  },
  methods: {
    //二维码
    creatQrCode(row) {
      this.dialogTableVisible = true;
      $(".qrcode").html("");
      setTimeout(() => {
        new QRCode(this.$refs.qrCodeUrl, {
          text: row, // 需要转换为二维码的内容
          width: 260,
          height: 260,
          colorDark: "#000000",
          colorLight: "#ffffff",
          correctLevel: QRCode.CorrectLevel.H,
        });
      }, 0);
    },
    //显示
    list() {
      GetShowEquipmentLedger(this.formInline).then((res) => {
        this.tableData = res.data.data;
        this.count = res.data.totalCount;
      });
    },
    handleSizeChange(val) {
      this.formInline.pageSize = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.formInline.pageIndex = val;
      this.list();
    },
    totalcount() {
      GetTotaocount().then((res) => {
        this.countnum = res.data;
      });
    },
    //全选/反选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //删除
    del() {
      if (this.multipleSelection.length <= 0) {
        this.$message.error("请选择一条数据");
        return;
      }

      this.multipleSelection.forEach((res) => {
        this.ids.id = res.id;
      });
      deleteEquipmentLedger(this.ids).then((res) => {
        if (res.data) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.list();
        } else {
          this.$message.error("删除失败");
          return;
        }
      });
    },
    //查看详情
    examine() {
      if (this.multipleSelection.length != 1) {
        this.$message.error("请选择一条数据");
        return;
      }
      this.dialogFormVisible1 = true;
      this.multipleSelection.forEach((res) => {
        this.form = res;
      });
    },
    //修改
    upd() {
      if (this.multipleSelection.length != 1) {
        this.$message.error("请选择一条数据");
        return;
      }
      this.dialogFormVisible = true;
      this.multipleSelection.forEach((res) => {
        this.form = res;
      });
    },
    baocun() {
      updEquipmentLedger(this.form).then((res) => {
        if (res.data) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.list();
          this.dialogFormVisible = false;
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    //跳转
    add() {
      this.$router.push("/AssetManage/AddEquipmentLedger");
    },

    // ----------以下为导出Excel数据功能--------------
    exportToExcel() {
      //excel数据导出
      require.ensure([], () => {
        const { export_json_to_excel } = require("../../excel/Export2Excel");
        const tHeader = [
          "序号",
          "设备编码",
          "设备名称",
          "设备类型",
          "型号",
          "品牌",
          "设备位置",
          "维护周期",
          "维护部门",
          "设备状态",
        ];
        const filterVal = [
          "id",
          "equipmentCoding",
          "equipmentName",
          "deviceType",
          "equipmentModel",
          "equipmentBrand",
          "equipmentLocaId",
          "mainteCycle",
          "mainteDepartId",
          "state",
        ];
        const list = this.tableData;
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, "设备台账excel");
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) => filterVal.map((j) => v[j]));
    },
  },
};
</script>
<style>
</style>